package com.mysoft.gfyx.marketdevelop.service.impl;

import com.mysoft.framework.modeling.dto.ListDataResult;
import com.mysoft.framework.modeling.dto.LoadDataParams;
import com.mysoft.framework.service.AppService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class BaseDataApiServiceImp extends AppService {
    public ListDataResult loadData(LoadDataParams options) {
        return null;
    }

    public List<Map<String, Object>> export(LoadDataParams options) {
        List<Map<String, Object>> exportData = new ArrayList<>();
        if (options.getPageIndex() == -1) {
            for (int i = 0; i < 5; i++) {
                // 因为数芯最多就支持2000，所以没必要更多了
                options.setPageIndex(i);
                options.setPageSize(2000);
                ListDataResult res = loadData(options);
                if (res.getTotal() > 0) {
                    exportData.addAll((List<Map<String, Object>>) res.getData());
                }
                // 如果查询没有数据，或者总数小于当前的分页数，那就退出循环
                if (res.getTotal() <= 0 || (res.getTotal() - ((i + 1) * 2000) <= 0)) {
                    break;
                }
            }
        } else {
            ListDataResult data = loadData(options);
            if (data.getTotal() >= 0) {
                exportData = (List<Map<String, Object>>) data.getData();
            }
        }
        return exportData;
    }
}
